﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ras2ClassLib;

namespace Ras2Web
{
    public partial class Song : System.Web.UI.Page
    {
        /// Id númer þess lags sem er verið að biðja um
        private int SongID;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Sækjum síðan lagið í Page_Load
            int songID = GetSongID();

            Ras2DBDataContext db = new Ras2DBDataContext();

            // Sækjum info um lagið
            LoadInfo(db);

            HitListController ctrl = new HitListController();

            m_gridHitListsWithSong.DataSource = ctrl.GetAllHitListsWithSong(songID);
            m_gridHitListsWithSong.DataBind();
        }
        private int GetSongID()
        {
            string id = Request.Params["id"] as string;

            if (!String.IsNullOrEmpty(id))
                this.SongID = int.Parse(id);
            else
                Response.Redirect("~/Default.aspx", true);
            return int.Parse(id);
        }
        private void LoadInfo(Ras2DBDataContext db)
        {
            // Sækja lag út frá id númeri
            var song = db.Songs.Single(a => a.ID == this.SongID);

            // Ef lag finnst þá fyllum við inn í control-in
            if (song != null)
            {
                // Setja titil á síðu
                Page.Title += " &rsaquo; " + song.Name;

                // Nafn og lýsing
                litSongName.Text = song.Name;
                litSongDescription.Text = !String.IsNullOrEmpty(song.Description) 
                    ? song.Description.Replace("\r\n", "<br />") : "Engin lýsing";
                litPerformerName.Text = song.Performer.Name;
                litFile.Text = !String.IsNullOrEmpty(song.MP3Path) 
                    ? song.MP3Path : "Engin skrá fannst..";
                
                // Búum til krækjur fyrir síðu
                litPerformerUrl.NavigateUrl = "~/Performer.aspx?ID=";
                litPerformerUrl.NavigateUrl += song.PerformerID;
                
                // Ef MP3 skrá er til staðar skal krækja á skránna
                if (!String.IsNullOrEmpty(song.MP3Path))
                {
                    litFileUrl.NavigateUrl = song.MP3Path;
                }

            }
        }
    }
}

